<?php

namespace common\models;

use common\base\BModel;
class LianTypeModel extends BModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'lian_type';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'parent_id','type'], 'integer'],
            [['name','url'], 'string', 'max' => 10],
            [['name','url'], 'unique'],
            [['name','url'], 'required', 'on' => ['add'], 'message' => '{attribute}不能为空'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '类型名称',
            'url' => 'url',
            'type'=> '分类级别',
            'parent_id'=> '父级id'
        ];
    }

    /**
     * 保存前
     * @param bool $insert
     * @return bool
     */
    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            $time = time();
            if($this->isNewRecord){
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }

    public function getSubType()
    {
        return $this->hasMany(self::className(), ['parent_id' => 'id']);
    }

}
